﻿// set up for all internal pages
function onInternalPageReady() {
  $(".btn").button();
  $("input").focus(function () {
    $(this).addClass("focus");
  }).blur(function () {
    $(this).removeClass("focus");
  });
  $(".section-header").live('click', function () {
    var link = $(this);
    link.find(".ui-icon:first").toggleClass('ui-icon-triangle-1-e').toggleClass('ui-icon-triangle-1-s');
    link.parents('.section:first').find(".content:first").toggle();
    resetMenu();
  });
  // if there is a message to flash, go ahead and show it for 10 seconds
  var reSpaces = /^\s*$/;
  if (!reSpaces.test($("#flashMsg").html())) {
    $("#flash").fadeIn('slow');
    setTimeout(function () { $("#flash").fadeOut('slow'); }, 1000 * 10);
  }
  if ($("#messages div").length > 0) {
    setTimeout(function () { $("#messages").slideDown('slow'); }, 250);
  }
}

// open the modal confirmation dialog
function openConfirmationModal(msg, fnOnOk) {
  $("#modalMsg").html(msg);
  // unbind all previous click handlers and add the new one
  $("#modal .btnOk").unbind('click').click(fnOnOk);
  $("#modal").dialog({ modal: true, width: 400 }).dialog('open');
}

// close the modal confirmation dialog
function closeConfirmationModal() {
  $("#modal").dialog('close');
}

// delete a quiz
function deleteQuizDefinition(id) {
  var fnSuccess = function () {
    setTimeout(function () {
      $("#modal a").fadeOut('fast');
    }, 250);
    $("#deleteQuizDefinitionId").val(id);
    $("#deleteQuizDefinitionForm").submit();
  };
  openConfirmationModal("Are you sure you want to delete this Quiz Definition?", fnSuccess);
  return false;
}

// delete a quiz
function deleteQuiz(id) {
  var fnSuccess = function () {
    setTimeout(function () {
      $("#modal a").fadeOut('fast');
    }, 250);
    $("#deleteQuizId").val(id);
    $("#deleteQuizForm").submit();
  };
  openConfirmationModal("Are you sure you want to delete this Quiz?", fnSuccess);
  return false;
}

// end a quiz
function endQuiz(id) {
  var fnSuccess = function () {
    setTimeout(function () {
      $("#modal a").fadeOut('fast');
    }, 250);
    $("#endQuizId").val(id);
    $("#endQuizForm").submit();
  };
  openConfirmationModal("Are you sure you want to end this Quiz?", fnSuccess);
  return false;
}

function clearQuizResults(id) {
  $("#clearQuizResultsId").val(id);
  $("#clearQuizResultsForm").submit();
}

function resetMenu() {
  $(".fg-button").each(function () {
    $(this).menu({ content: $(this).next().html(), width: 125 });
  });
}
